package com.xy.bizportdemo.messagelist;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewStub;
import android.widget.TextView;

import com.example.messageutil.iface.IMessageProcessor;
import com.example.messageutil.iface.ISmartSmsItemHolder;
import com.xy.bizportdemo.R;
import com.xy.bizportdemo.controller.SmartSMSManager;
import com.xy.bizportdemo.database.entity.MessageItem;
import com.xy.bizportdemo.util.TimeCacheUtil;

public class MessageItemViewHolder implements ISmartSmsItemHolder {
    private static final String TAG = "ItemViewHolder";

    private View mViewRoot = null;
    private Context mContext = null;
    private MessageItem mMessageItem = null;
    private SendViewHolder mSendViewHolder = null;
    private ReceiveViewHolder mReceiveViewHolder = null;
    private boolean mIsRich;
    private Handler mHandler = null;

    public MessageItemViewHolder(Context context, View view, boolean isRich, Handler handler) {
        mContext = context;
        mViewRoot = view;
        mIsRich = isRich;
        mHandler = handler;
    }

    private String getFormatDate(long time) {
        return TimeCacheUtil.getInstance().formateTime(time);
    }

    private class SendViewHolder {
        View viewRoot;
        TextView textViewContent;
        TextView textViewTime;
    }

    private class ReceiveViewHolder {
        View viewRoot;
        TextView textViewTime;
        TextView textViewContent;
        View viewTopContent;
    }

    private void inflateViewSend() {
        if (mSendViewHolder == null) {
            mSendViewHolder = new SendViewHolder();
            View view = ((ViewStub) mViewRoot.findViewById(R.id.viewstub_send)).inflate();
            mSendViewHolder.viewRoot = view;
            mSendViewHolder.textViewContent = (TextView) view.findViewById(R.id.messagelist_item_content_send);
            mSendViewHolder.textViewTime = (TextView) view.findViewById(R.id.messagelist_item_sendtime_textview);
        }

        mSendViewHolder.textViewContent.setText(mMessageItem.getSmsBody());
        mSendViewHolder.textViewTime.setText(getFormatDate(mMessageItem.getSmsReceiveTime()));
        mSendViewHolder.viewRoot.setVisibility(View.VISIBLE);
    }

    private void inflateViewReceive() {
        if (mReceiveViewHolder == null) {
            mReceiveViewHolder = new ReceiveViewHolder();
            View view = ((ViewStub) mViewRoot.findViewById(R.id.viewstub_receive)).inflate();
            mReceiveViewHolder.viewRoot = view;
            mReceiveViewHolder.viewTopContent = view.findViewById(R.id.message_block);
            mReceiveViewHolder.textViewTime = (TextView) view.findViewById(R.id.messagelist_item_rectime_textview);
            mReceiveViewHolder.textViewContent = (TextView) view.findViewById(R.id.messagelist_item_content_textview);
        }
        mReceiveViewHolder.textViewTime.setText(getFormatDate(mMessageItem.getSmsReceiveTime()));
        mReceiveViewHolder.viewRoot.setVisibility(View.VISIBLE);
        bindRichBubbleView();
    }

    private void hideAll() {
        if (mReceiveViewHolder != null && mReceiveViewHolder.viewRoot != null) {
            mReceiveViewHolder.viewRoot.setVisibility(View.GONE);
        }

        if (mSendViewHolder != null && mSendViewHolder.viewRoot != null) {
            mSendViewHolder.viewRoot.setVisibility(View.GONE);
        }
    }

    public void bind(MessageItem item, int position) {
        if (item == null) {
            Log.w(TAG, "bind: item null");
            return;
        }

        hideAll();
        mMessageItem = item;
        if (mMessageItem.getBoxId() == MessageItem.MESSAGE_TYPE_OUTBOX) {
            inflateViewSend();
        } else {
            inflateViewReceive();
        }
    }

    private IMessageProcessor mMessageProcessor = null;

    private void bindRichBubbleView() {
        if (mMessageProcessor == null) {
            mMessageProcessor = SmartSMSManager.getInstance().loadMessageProcessor();
            mMessageProcessor.setupProcessor(mContext, mHandler, this);
        }

        mMessageProcessor.bind(mMessageItem);
//        if (mCardHelper == null) {
//            SmartSmsCardOptions options = new SmartSmsCardOptions.Builder()
//                    .setContentId(R.id.messagelist_item_content_textview)
//                    .setRichBubbleId(R.id.duoqu_rich_item_group)
//                    .setSimpleBubbleId(R.id.duoqu_simple_bubble_action_group)
//                    .setHandler(mHandler)
//                    .build();
//            mCardHelper = new SmartSmsCardHelper(this, options);
//        }
//
//        mCardHelper.bind(mMessageItem);
//        if (mBubbleViewHolder == null) {
//            mBubbleViewHolder = new BubbleViewHolder(this);
//            Map<String, String> map = new HashMap<>();
//            map.put(BubbleViewHolder.ID_TOP, String.valueOf(R.id.message_block));
//            map.put(BubbleViewHolder.ID_SIMPLE, String.valueOf(R.id.duoqu_simple_bubble_action_group));
//            if (mIsRich) {
//                map.put(BubbleViewHolder.ID_RICH, String.valueOf(R.id.duoqu_rich_item_group));
//            }
//            map.put(BubbleViewHolder.ID_CONTENT, String.valueOf(R.id.messagelist_item_content_textview));
//            //map.put(BubbleViewHolder.ID_CHANGE, String.valueOf(R.id.change_richbubble));
//            map.put(BubbleViewHolder.FUNC_BOTTOM_CHANGE, "true");
//            mBubbleViewHolder.setMap(map);
//        }
//
//        mBubbleViewHolder.bind(mMessageItem);
    }


//    @Override
//    public boolean isGifVisible() {
//        return false;
//    }

    /**
     * 显示原文
     *
     * @param hasBubble 是否卡片化，true表示是
     */
    @Override
    public void showDefaultItem(boolean hasBubble) {
        if (mMessageItem.getBoxId() == MessageItem.MESSAGE_TYPE_OUTBOX) {
            mSendViewHolder.textViewContent.setText(mMessageItem.getSmsBody());
        } else {
            mReceiveViewHolder.textViewContent.setText(mMessageItem.getSmsBody());
            mReceiveViewHolder.viewTopContent.setBackgroundResource(R.drawable.default_message_item_bg_nobtn);
        }
    }

    @Override
    public int getRichBubbleId() {
        return R.id.duoqu_rich_item_group;
    }

    @Override
    public int getSimpleBubbleId() {
        return R.id.duoqu_simple_bubble_action_group;
    }

    @Override
    public int getContentId() {
        return R.id.messagelist_item_content_textview;
    }

    @Override
    public View getRootView() {
        return mViewRoot;
    }

    @Override
    public View getParentView() {
        return null;
    }
}
